package com.guli.eduService.controller;


import com.guli.commonUtils.R;
import com.guli.eduService.entity.EduSubject;
import com.guli.eduService.entity.vo.OneSubjectInfo;
import com.guli.eduService.service.impl.EduSubjectServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-02
 */
@Api(description = "课程分类管理")
@RestController
@RequestMapping("/eduService/subject")
//@CrossOrigin // 解决跨域问题
public class EduSubjectController {

    @Autowired
    private EduSubjectServiceImpl eduSubjectService;

    // 即，传入excel文件，向数据库中添加课程分类信息;
    @ApiOperation(value = "添加课程分类")
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file){
        eduSubjectService.addSubject(file , eduSubjectService);
        return R.ok();
    }

    @ApiOperation(value = "获取所有课程分类信息")
    @GetMapping("getAllSubjects")
    public R getAllSubjects(){
        List<OneSubjectInfo> result = eduSubjectService.getAllSubjects();
        if(result != null){
            return R.ok().data("SubjectsInfo" , result);
        }
        else {
            return R.error().message("未获取到课程分类信息,请先导入课程分类信息!!!");
        }
    }

    @ApiOperation(value = "根据一级分类id，来获取一级分类数据")
    @GetMapping("/getOneSubject/{oneSubjectId}")
    public R getOneSubject(
            @ApiParam(name = "oneSubjectId" , value = "一级分类ID" , required = true)
            @PathVariable String oneSubjectId){
        // 根据id，来获取指定一级分类信息：
        OneSubjectInfo oneSubjectInfo = eduSubjectService.getSubjectById(oneSubjectId);
        return R.ok().data("oneSubjectInfo" , oneSubjectInfo);
    }
}

